package com.shbaiche.bifenba.ui.my;

import android.app.Activity;
import android.app.Dialog;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;
import android.view.View;
import android.widget.ListView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.sevenheaven.segmentcontrol.SegmentControl;
import com.shbaiche.bifenba.App;
import com.shbaiche.bifenba.R;
import com.shbaiche.bifenba.adapter.my.MoreFollowAdapter;
import com.shbaiche.bifenba.model.base.BoolValue;
import com.shbaiche.bifenba.model.base.TransMessage;
import com.shbaiche.bifenba.model.user.FollowInfoVo;
import com.shbaiche.bifenba.model.user.FollowListMessage;
import com.shbaiche.bifenba.model.user.FollowListVo;
import com.shbaiche.bifenba.model.user.FollowUpdateMessage;
import com.shbaiche.bifenba.net.JsonCallback;
import com.shbaiche.bifenba.net.NetApi;
import com.shbaiche.bifenba.ui.base.BaseActivity;
import com.shbaiche.bifenba.util.StringUtil;
import com.shbaiche.bifenba.util.ToastUtil;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import cn.droidlover.xdroidbase.router.Router;
import cn.droidlover.xrecyclerview.XRecyclerView;
import okhttp3.Call;


public class MoreFollowActivity extends BaseActivity {
    @BindView(R.id.toolbar)
    Toolbar toolbar;

    @BindView(R.id.more_list)
    ListView lv;

    @BindView(R.id.segment_control)
    SegmentControl sc;

    @BindView(R.id.done)
    TextView doneTv;

    MoreFollowAdapter adapter;

    List<List<FollowInfoVo>> listData;
    List<Map<Integer, BoolValue>> listDataMap;

    Dialog dg;

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == android.R.id.home) {
            finish();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }


    @Override
    public void initData(Bundle savedInstanceState) {

        initAdapter();
        dg = new Dialog(this);
        dg.setContentView(R.layout.view_loading);
        loadData(1);

        doneTv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                updateFollow();
            }
        });
    }

    @Override
    public void setListener() {

    }

    private void initAdapter() {
        lv.setAdapter(getAdapter());
    }

    public void loadData(final int page) {
        dg.show();

        FollowListMessage msg = new FollowListMessage();
        msg.setPassport(App.getPassport());
        NetApi.FollowList(msg, new JsonCallback<TransMessage>() {

            @Override
            public void onFail(Call call, Exception e, int id) {
                dg.hide();
            }

            @Override
            public void onError(Call call, Exception e, int id) {
                ToastUtil.show("onFail" + e.getMessage());
                dg.hide();
            }

            @Override
            public void onResponse(TransMessage response, int id) {
                dg.hide();
                Gson gson = new Gson();
                FollowListMessage msg = gson.fromJson(response.getMessage(), FollowListMessage.class);

                if (StringUtil.equalsIgnoreCase(TransMessage.SuccessCode, msg.getCode())) {
                    String[] scArray = null;
                    listData = new ArrayList<List<FollowInfoVo>>();
                    listDataMap = new ArrayList<Map<Integer, BoolValue>>();

                    List<FollowListVo> list = msg.getVoList();
                    scArray = new String[list.size()];
                    for (int i = 0; i < list.size(); i++) {
                        FollowListVo vo = list.get(i);
                        scArray[i] = vo.getName();
                        List<FollowInfoVo> bList = vo.getBkList();
                        List<FollowInfoVo> fList = vo.getFtList();
                        List<FollowInfoVo> result = new ArrayList<FollowInfoVo>();
                        if (bList != null) {
                            result.addAll(bList);
                        }
                        if (fList != null) {
                            result.addAll(fList);
                        }
                        listData.add(result);
                    }

                    Map<Integer, BoolValue> isFollowMap;
                    for (int i = 0; i < listData.size(); i++) {
                        isFollowMap = new HashMap<>();
                        List<FollowInfoVo> l = listData.get(i);
                        for (int j = 0; j < l.size(); j++) {
                            isFollowMap.put(j, l.get(j).getIsFollow());
                        }
                        listDataMap.add(isFollowMap);
                    }

                    if (scArray != null && scArray.length > 0) {
                        sc.setText(scArray);
                    }
                    if (listData != null && listData.size() > 0) {
                        getAdapter().isFollowMap = listDataMap.get(0);
                        getAdapter().setData(listData.get(0));
                    }
                    sc.setOnSegmentControlClickListener(new SegmentControl.OnSegmentControlClickListener() {
                        @Override
                        public void onSegmentControlClick(int index) {
                            if (listData != null) {
                                List<FollowInfoVo> mData = listData.get(index);
                                try {
                                    listDataMap.set(getAdapter().index, getAdapter().isFollowMap);
                                    getAdapter().isFollowMap = listDataMap.get(index);
                                    getAdapter().setData(mData);
                                    getAdapter().index = index;
                                } catch (Exception e) {

                                }
                            }
                        }
                    });

                } else {
                }
            }
        });

    }

    private void updateFollow() {
        dg.show();

        if (getAdapter().isFollowMap == null)
            return;
        if (getAdapter().his == null)
            return;


        List<String> bList = new ArrayList<>();
        List<String> fList = new ArrayList<>();

        for (int k = 0; k < listData.size(); k++) {
            Map<Integer, BoolValue> isFollowMap = listDataMap.get(k);
            List<FollowInfoVo> updaList = listData.get(k);

            for (int i = 0; i < updaList.size(); i++) {
                if (isFollowMap.get(i).getValue()) {
                    String gametype = "1";
                    if (updaList.get(i).getGameType() != null)
                        gametype = updaList.get(i).getGameType();
                    if ("1".equals(gametype)) {
                        fList.add(updaList.get(i).getFollowId());
                    } else {
                        bList.add(updaList.get(i).getFollowId());
                    }
                }
            }
        }


        FollowUpdateMessage msg = new FollowUpdateMessage();
        msg.setPassport(App.getPassport());
        msg.setBkFollowId((bList.toArray(new String[bList.size()])));
        msg.setFtFollowId(fList.toArray(new String[fList.size()]));
        msg.setType(FollowUpdateMessage.FollowType.TEAM);
        NetApi.FollowUpdate(msg, new JsonCallback<TransMessage>() {

            @Override
            public void onFail(Call call, Exception e, int id) {
                dg.hide();
            }

            @Override
            public void onError(Call call, Exception e, int id) {
                ToastUtil.show("修改失败!" + e.getMessage());
                dg.hide();
            }

            @Override
            public void onResponse(TransMessage response, int id) {
                dg.hide();
                Gson gson = new Gson();
                FollowUpdateMessage msg = gson.fromJson(response.getMessage(), FollowUpdateMessage.class);

                if (StringUtil.equalsIgnoreCase(TransMessage.SuccessCode, msg.getCode())) {
                    ToastUtil.show(msg.getMessage() + "");
                } else {
                    ToastUtil.show(msg.getMessage() + "");
                }
            }
        });
    }

    public MoreFollowAdapter getAdapter() {
        if (adapter == null) {
            adapter = new MoreFollowAdapter(context);
        }
        return adapter;
    }


    public void setLayoutManager(XRecyclerView recyclerView) {
        recyclerView.verticalLayoutManager(context);
    }


    public static void launch(Activity activity) {
        Router.newIntent()
                .from(activity)
                .to(MoreFollowActivity.class)
                .data(new Bundle())
                .launch();
    }

    @Override
    protected void onDestroy() {
        if (dg != null) {
            dg.dismiss();
            dg = null;
        }
        super.onDestroy();
    }

    @Override
    public int getLayoutId() {
        return R.layout.fragment_follow_more_team;
    }

    @Override
    public void setToolbar() {
        initToolbar(toolbar, null);
    }
}
